package com.iss.supermarket.PersonalInfoManageUI;
import com.iss.supermarket.util.MessageWindow.MsgWin;
import com.iss.supermarket.util.entity.SpUser;
import com.iss.supermarket.util.repository.UserDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PersonInfoUI extends JFrame{
    //单例模式
    private static PersonInfoUI personInfoUI;
    //定义组件
    private JTextField[] personalInfoTexts;
    private JLabel[] personalInfoLabels;
    private JButton jButton1;
    private JPanel personInfoUIPanel;
    private SpUser thisUser;

    //连接数据库
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/spring_ioc.xml");
    UserDao userDao = ctx.getBean("UserDao", UserDao.class);

    public PersonInfoUI(SpUser spUser){
        thisUser = spUser;
        InitAssembly(thisUser);
        InitUI();
    }

    private void InitUI(){
//        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        for(int i=0;i<8;++i){
            JPanel tmpPanel = new JPanel();
            tmpPanel.add(personalInfoLabels[i]);
            tmpPanel.add(personalInfoTexts[i]);
            verticalBox.add(tmpPanel);
        }

        JPanel jPanel1 = new JPanel();
        jPanel1.add(jButton1);
        verticalBox.add(jPanel1);

        personInfoUIPanel.add(verticalBox);

//        container.add(returnPanel);
//        setSize(500,700);
//        setVisible(true);
    }

    private void InitAssembly(SpUser thisUser){

        personalInfoLabels = new JLabel[8];
        personalInfoLabels[0] = new JLabel("用户UUID");
        personalInfoLabels[0].setHorizontalAlignment(SwingConstants.CENTER);
        personalInfoLabels[0].setPreferredSize(new Dimension(50,25));

        personalInfoLabels[1] = new JLabel("用户名：");
        personalInfoLabels[2] = new JLabel("用户密码：");
        personalInfoLabels[3] = new JLabel("用户邮箱：");
        personalInfoLabels[4] = new JLabel("用户手机号码：");
        personalInfoLabels[5] = new JLabel("昵称：");
        personalInfoLabels[6] = new JLabel("角色：");
        personalInfoLabels[7] = new JLabel("创建时间：");

        for(int i=0;i<personalInfoLabels.length;++i){
            personalInfoLabels[i].setHorizontalAlignment(SwingConstants.CENTER);
            personalInfoLabels[i].setPreferredSize(new Dimension(150,25));
            personalInfoLabels[i].setFont(new Font("微软雅黑",Font.PLAIN,16));
        }

        personalInfoTexts = new JTextField[8];
        //用户uuid不能由用户修改
        personalInfoTexts[0] = new JTextField(thisUser.getUserUuid());
        personalInfoTexts[0].setEditable(false);

        personalInfoTexts[1] = new JTextField();
        personalInfoTexts[1].setText(thisUser.getUserName());

        personalInfoTexts[2] = new JTextField();
        personalInfoTexts[2].setText(thisUser.getPassword());

        personalInfoTexts[3] = new JTextField();
        personalInfoTexts[3].setText(thisUser.getEmail());

        personalInfoTexts[4] = new JTextField();
        personalInfoTexts[4].setText(thisUser.getMobile());

        personalInfoTexts[5] = new JTextField();
        personalInfoTexts[5].setText(thisUser.getNiceName());

        //用户角色不能由用户修改
        personalInfoTexts[6] = new JTextField();
        personalInfoTexts[6].setText(thisUser.getRemark());
        personalInfoTexts[6].setEditable(false);

        //用户创建时间不能由用户修改
        personalInfoTexts[7] = new JTextField(thisUser.getModtime());
        personalInfoTexts[7].setEditable(false);

        for(int i=0;i<personalInfoTexts.length;++i){
            personalInfoTexts[i].setHorizontalAlignment(SwingConstants.CENTER);
            personalInfoTexts[i].setPreferredSize(new Dimension(350, 30));
            personalInfoTexts[i].setText(personalInfoTexts[i].getText().trim());
            personalInfoTexts[i].setFont(new Font("微软雅黑",Font.PLAIN,14));
        }

        jButton1 = new JButton("保存");
        jButton1.addActionListener(new ButtonAction());

        personInfoUIPanel = new JPanel();
        personInfoUIPanel.setPreferredSize(new Dimension(600,600));
    }

    private class Check{
        //检查类
        private static final String MOBILE_REGEX_EXP = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[0-9])|(18[0-9])|(19[1,8,9]))\\d{8}$";
        private static final String EMAIL_REGEX_EXP = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        private static final String PASSWORD_REGEX_EXP = "^([a-z0-9A-Z]){8,14}";

        private boolean checkUserName() {
            if ("".equals(personalInfoTexts[1].getText())) {
                //单例模式此处不适用，MsgWin调用一次单例模式后，单例模式的对象一直不为空
                MsgWin msgWin = new MsgWin("用户名不能为空");
                return false;
            }
            return true;
        }

        private boolean checkUserEmail(){
            if ("".equals(personalInfoTexts[3].getText())) {
                MsgWin msgWin2 = new MsgWin("邮箱不能为空");
                return false;
            }
            Pattern pattern = Pattern.compile(EMAIL_REGEX_EXP);
            Matcher matcher = pattern.matcher(personalInfoTexts[3].getText());
            if (!matcher.matches()) {
                MsgWin msgWin2 = new MsgWin("邮箱格式错误");
                return false;
            }
            return true;
        }

        private boolean checkUserMobile() {
            if ("".equals(personalInfoTexts[4].getText())) {
                MsgWin msgWin3 = new MsgWin("手机号码不能为空");
                return false;
            }
            Pattern pattern = Pattern.compile(MOBILE_REGEX_EXP);
            Matcher matcher = pattern.matcher(personalInfoTexts[4].getText());
            if (!matcher.matches()) {
                MsgWin msgWin3 = new MsgWin("手机号码格式有误");
                return false;
            }
            return true;
        }

        private boolean checkUserPassword(){
            if("".equals(personalInfoTexts[2].getText())){
                MsgWin msgWin4 = new MsgWin("请输入密码");
                return false;
            }
            Pattern pattern = Pattern.compile(PASSWORD_REGEX_EXP);
            Matcher matcher = pattern.matcher(personalInfoTexts[2].getText());
            if(!matcher.matches()){
                MsgWin msgWin = new MsgWin("输入密码应只包含大写字母，小写字母和数字");
                return false;
            }
            return true;
        }
    }

    private class ButtonAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if("保存".equals(e.getActionCommand())){
                //生成检查类
                Check check = new Check();
                //检查用户新的个人信息的合法性
                if(check.checkUserEmail() && check.checkUserMobile() && check.checkUserPassword() && check.checkUserName()) {
                    userDao.updateUserInfo(personalInfoTexts[1].getText(),
                            personalInfoTexts[2].getText(),
                            personalInfoTexts[4].getText(),
                            personalInfoTexts[3].getText(),
                            personalInfoTexts[5].getText(),
                            personalInfoTexts[0].getText(),
                            personalInfoTexts[6].getText(),
                            personalInfoTexts[7].getText(),
                            0);
                }
                MsgWin msgWin = new MsgWin("修改成功");
            }
        }
    }

    public static synchronized PersonInfoUI getPersonInfoUI(SpUser spUser){
        if(personInfoUI == null){
            personInfoUI = new PersonInfoUI(spUser);
        }
        return personInfoUI;
    }

    public JPanel getPersonInfoUIPanel(){
        return personInfoUIPanel;
    }
}
